package com.memory.modules.business.service.earn.impl;

import com.memory.common.data.PageSort;
import com.memory.modules.business.domain.earn.DayEarn;
import com.memory.modules.business.repository.earn.DayEarnRepository;
import com.memory.modules.business.service.earn.DayEarnService;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Optional;

/**
 * @PackageName: com.memory.modules.business.service.earn
 * @ClassName: DayEarnService
 * @Description:
 * @author: zhuguangxun
 * @date 2021/3/11
 */
@Service
@RequiredArgsConstructor
public class DayEarnServiceImpl implements DayEarnService {

    private final DayEarnRepository dayEarnRepository;

    @Override
    public DayEarn create(DayEarn earn) {
        return dayEarnRepository.save(earn);
    }

    @Override
    public DayEarn modify(Long id, DayEarn earn) {
        return findById(id).map(e -> {
            earn.setId(id);
            dayEarnRepository.save(earn);
            return earn;
        }).orElse(null);
    }

    @Override
    public DayEarn remove(Long id) {
        return findById(id).map(earn -> {
            dayEarnRepository.delete(earn);
            return earn;
        }).orElse(null);
    }

    @Override
    public Optional<DayEarn> findById(Long id) {
        return dayEarnRepository.findById(id);
    }

    @Override
    public DayEarn findByDate(Date date) {
        return dayEarnRepository.findByCreateDate(date);
    }

    @Override
    public Page<DayEarn> findPage(Example<DayEarn> example) {
        PageRequest page = PageSort.pageRequest();
        return dayEarnRepository.findAll(example, page);
    }
}
